﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using RestaurateurConsole.Core;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace RestaurateurConsole
{
    public class SerializationHandler
    {
        private string SerializationPath;

        private SerializationHandler() { }
        public SerializationHandler(string path, string folder, string name)
        {
            SerializationPath = path + "\\" + folder + "\\" + name;
            if (!Directory.Exists(path + "\\" + folder))
                Directory.CreateDirectory(path + "\\" + folder);
        }

        public SerializationHandler(string path, string name)
        {
            SerializationPath = path + "\\" + name;
        }

        public void Serialize<T>(T objectRoot)
        {
            FileStream fs = File.Open(SerializationPath, FileMode.Create);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(fs, objectRoot);
            fs.Close();
        }

        public T DeSerialize<T>() where T : class
        {
            FileStream fs = File.Open(SerializationPath, FileMode.Open);
            BinaryFormatter bf = new BinaryFormatter();
            T dishes = bf.Deserialize(fs) as T;
            fs.Close();
            return dishes;
        }
    }
}
